public class Complex {
    private final double re;   // the real part
    private final double im;   // the imaginary part

    // create a new object with the given real and imaginary parts
    public Complex(double real, double imag) {
        re = real;
        im = imag;
    }

    // return a string representation of the invoking Complex object
    public String toString() {
        if (getIm() == 0) return getRe() + "";
        if (getRe() == 0) return getIm() + "i";
        if (getIm() <  0) return getRe() + " - " + (-getIm()) + "i";
        return getRe() + " + " + getIm() + "i";
    }

    // return a new Complex object whose value is (this + b)
    public Complex plus(Complex b) {
        Complex a = this;             // invoking object
        double real = a.getRe() + b.getRe();
        double imag = a.getIm() + b.getIm();
        return new Complex(real, imag);
    }

    public Complex minus(Complex b) {
        Complex a = this;
        double real = a.getRe() - b.getRe();
        double imag = a.getIm() - b.getIm();
        return new Complex(real, imag);
    }

    public Complex times(Complex b) {
        Complex a = this;
        double real = a.getRe() * b.getRe() - a.getIm() * b.getIm();
        double imag = a.getRe() * b.getIm() + a.getIm() * b.getRe();
        return new Complex(real, imag);
    }

    public Complex times(double alpha) {
        return new Complex(alpha * getRe(), alpha * getIm());
    }


    // return the real or imaginary part
    public double re() { return getRe(); }
    public double im() { return getIm(); }



    public static Complex plus(Complex a, Complex b) {
        double real = a.getRe() + b.getRe();
        double imag = a.getIm() + b.getIm();
        Complex sum = new Complex(real, imag);
        return sum;
    }

	public double getRe() {
		return re;
	}

	public double getIm() {
		return im;
	}
}

    